library IlluminationRounds initializer init requires LegacySystem

	//===========================================================================
	// Ability, Dummy Unit, and sound pointers here
	//===========================================================================

	private function Trig_Illumination_Rounds_Conditions takes nothing returns boolean
		if ( not ( GetSpellAbilityId() == 'A02Z' ) ) then
			return false
		endif
		return true
	endfunction

	// creates flare effect and reveals area
	private function illuminate takes unit caster, real x, real y returns nothing
		// create flare effect
		call DestroyEffect(AddSpecialEffect("Abilities\\Spells\\Human\\Flare\\FlareTarget.mdl",x,y))
	endfunction

	private function reveal takes real x, real y, integer time returns nothing
		local integer i = 0
		local unit u 
	   
	    set u = CreateUnit(Player(0),'e001',x, y,0.0)
		call UnitApplyTimedLife(u,'Bhwd', I2R(time))
	
		loop
			 exitwhen i > 11
			 set u = CreateUnit(Player(i),'e002',x, y,0.0)
			 call UnitApplyTimedLife(u,'Bhwd',I2R(time))
			 set i = i + 1
		endloop
	endfunction


	private function playArtillerySoundIllumination takes real x, real y returns nothing
		local location l = Location(x+300.0*GetRandomReal(-0.7,0.7),y+300.0*GetRandomReal(-0.7,0.7))
		call RemoveLocation(l)
		set l = null
	endfunction

	
	private function Trig_Illumination_Rounds_Actions takes nothing returns nothing
		
		// variables
		local unit caster = GetTriggerUnit()
		local real x = GetSpellTargetX()
		local real y = GetSpellTargetY()
		local integer total_shells = getInteger(null,"FO_total_shells")
		local integer i = 0
		local integer number_of_shells = 5
		local integer barrage_delay = getInteger(null,"FO_barrage_delay") 
		local integer duration = 21
		
		local unit target_aura
	   
		// core
		// check for shells and substract number per barrage
		if(number_of_shells > total_shells) then 
			 call print ("Shells depleted")  
			 set target_aura = null
			 set caster = null
			 return
		else
			 call putInteger(null,"FO_total_shells", total_shells - number_of_shells)
			 call print("Shells Remaining: " + I2S(total_shells - number_of_shells))
		endif 
		
	   

		call TriggerSleepAction(barrage_delay-2)
	   
		// play sound 2 seconds before impact
		call playArtillerySoundIllumination(x,y)

		call TriggerSleepAction(2)
		
		call reveal(x,y, duration)

		// create illuminate dummy
		set i = 0
		loop
			 exitwhen i > 7
			 call illuminate(caster,x,y)  
			 call TriggerSleepAction(2.0)
			 set i = i + 1
		endloop
		// cleanup
		set target_aura = null
		set caster = null
	endfunction


	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger(  )
		call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_EFFECT )
		call TriggerAddCondition( t, Condition( function Trig_Illumination_Rounds_Conditions ) )
		call TriggerAddAction( t, function Trig_Illumination_Rounds_Actions )
	endfunction

endlibrary
